<template>
    <div class="background">
        <div style="width: 400px; margin: 25vh auto">
            <div style="color: rgb(255,255,255); font-size: 30px; text-align: center; font-weight: bold; padding: 30px 0">欢迎登录</div>
            <el-form :model="form" ref="form" size="normal" :rules="rules">
                <el-form-item prop="username">
                    <el-input v-model="form.username" placeholder="请输入账号">
                        <template #prefix>
                            <el-icon class="el-input__icon"><UserFilled /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="Lock" v-model="form.password" type="password" show-password placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input prefix-icon="Key" v-model="form.validCode" style="width: 50%;" placeholder="请输入验证码"> </el-input>
                    <ValidCode @input="createValidCode" />
                </el-form-item>
                <el-form-item style="padding-left: 120px">
                    <el-row>
                        <el-button type="primary" @click="login">登&nbsp;&nbsp;&nbsp;录</el-button>
                        <el-button type="primary" @click="register">注&nbsp;&nbsp;&nbsp;册</el-button>
                    </el-row>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import request from "../utils/request";
    import ValidCode from "../components/ValidCode";

    export default {
        name: "Login",
        components: {
            ValidCode,
        },
        data () {
            return {
                form: {},
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ],
                },
                validCode: '',
            }
        },
        methods: {
            // 接受验证码组件提交的四位验证码
            createValidCode(date){
                this.validCode = date;
            },
            login() {
                this.$refs['form'].validate((valid) => {
                    if (valid){
                        if (!this.form.validCode){
                            this.$message.error("请填写验证码");
                            return;
                        }
                        if (this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()){
                            this.$message.error("验证码错误");
                            return;
                        }
                        request.post("/user/login", this.form).then(res => {
                            if (res.code === '0'){
                                this.$message({
                                    type: "success",
                                    message: "登录成功"
                                })
                                sessionStorage.setItem("user", JSON.stringify(res.data))  //缓存用户信息
                                if (res.data.role===1){      //如果角色是管理员
                                    this.$router.push("/")   //登录成功之后进行页面跳转，跳转到主页用户界面
                                }else {  //身份不是管理员
                                    this.$router.push("/book")
                                }
                            } else {
                                this.$message({
                                    type: "error",
                                    message: res.msg
                                })
                            }
                        })
                    }
                })
            },
            register(){
                this.$router.push("/register")
            }
        }
    }
</script>

<style scoped>
    .background{
        width:100%;
        height:100%;
        background: url("../assets/login/bgimg.jpg") center;
        /*-webkit-filter: blur(5px);*/
        /*filter: blur(15px);*/
        position: fixed;
        z-index: -1;
        /*background-size:100% 100%;*/
    }
    ::v-deep(.el-input__inner) {
    }
</style>